package top.healthylife.gateway.platform.eye.api.api.jingyingxinxi;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.eye.api.config.SystemConfig;
import top.healthylife.gateway.platform.eye.api.util.OutHttpUtil;
import top.healthylife.gateway.platform.eye.dto.jingyingxinxi.EyeReqCustomerDto.req.EyeReqCustomerDto;
import top.healthylife.gateway.platform.eye.dto.jingyingxinxi.EyeReqCustomerDto.res.Clientsyear;
import top.healthylife.gateway.platform.eye.dto.jingyingxinxi.EyeReqCustomerDto.res.Eyerescustomerdto;

import java.util.List;

/**
 * @author: GenUtil
 * @date: 2020-12-15 19:07:24
 */
@Slf4j
@Service
@ApiServiceExplain(price = 20, docUrl = "http://open.tianyancha.com/open/947", apiName = "客户[947]", info = "可以通过公司名称或ID获取企业相关客户信息，企业相关客户信息包括客户、销售占比、销售金额、报告期等字段的详细信息")
public class EyeCustomerApi implements ParentFace<EyeReqCustomerDto, Eyerescustomerdto> {

    static final String URL = "http://open.api.tianyancha.com/services/open/m/customer/2.0";

    /**
     * 执行这个服务接口
     * 如果成功，返回成功结果，如果接口错误，直接抛异常即可
     *
     * @param eyeDto 请求参数
     * @return 查询的真实结果，如果是失败，则直接返回异常即可
     */
    @Override
    public Eyerescustomerdto execute(EyeReqCustomerDto eyeDto) {
        //可直接通过工具类获取这个对象
        SystemConfig o = (SystemConfig) SystemConfigUtils.get();
        Eyerescustomerdto eyerescustomerdto = OutHttpUtil.get(URL, BeanUtil.beanToMap(eyeDto), o, Eyerescustomerdto.class);
        if (eyerescustomerdto == null) {
            return new Eyerescustomerdto().setLength(0);
        }
        //fixme 扩展字段
        List<Clientsyear> clientsYear = eyerescustomerdto.getClientsYear();
        return eyerescustomerdto.setLength(ObjectUtil.isEmpty(clientsYear) ? 0 : clientsYear.size());

    }
}
